﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Admin1_Default : System.Web.UI.Page
{
    //Datacon db = new Datacon();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            InitData();

        }
    }
    protected void InitData()
    {
        //int SingleProblemID = int.Parse(Request["ID"].ToString());  //取出传递过来的试题编号
        //SingleProblem singleproblem = new SingleProblem();          //创建单选题对象
        //if (singleproblem.LoadData(SingleProblemID))                //如果取出题目信息，分别放在相应控件显示
        //{
        //    db.ecDropDownList(ddlCourse, "select *from Course where c_id=(select c_id from SingleProblem where ID='" + SingleProblemID + " ')", "c_name", "c_id");
        //    txtTitle.Text = singleproblem.Title;
        //    txtAnswerA.Text = singleproblem.AnswerA;
        //    txtAnswerB.Text = singleproblem.AnswerB;
        //    txtAnswerC.Text = singleproblem.AnswerC;
        //    txtAnswerD.Text = singleproblem.AnswerD;
        //    ddlAnswer.SelectedItem.Text = singleproblem.Answer;
        //}
        //else                //查询出错，给出提示
        //{
        //    Response.Write("<script>alert('加载数据错误！');</script>");
        //    Response.Write("<script>window.location.href='ChangeDanXuan'</script>");
        //}
        int SingleProblemID = int.Parse(Request["ID"].ToString());  //取出传递过来的试题编号
        using (DataClassesDataContext ds = new DataClassesDataContext())
        {
            var r = (from q in ds.SingleProblem
                     where q.ID == SingleProblemID
                     select q).FirstOrDefault();
            ddlCourse.DataSource = from q in ds.Course
                                   where q.c_id == r.c_id
                                   select q;
            ddlCourse.DataTextField = "c_name";
            ddlCourse.DataValueField = "c_id";
            ddlCourse.DataBind();
            txtTitle.Text = r.Title;
            txtAnswerA.Text = r.AnswerA;
            txtAnswerB.Text = r.AnswerB;
            txtAnswerC.Text = r.AnswerC;
            txtAnswerD.Text = r.AnswerD;
            ddlAnswer.SelectedItem.Text = r.Answer;
        }

    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        if (Page.IsValid)
        {
            //SingleProblem singleproblem = new SingleProblem();          //创建单选题对象
            ////singleproblem.c_id = int.Parse(ddlCourse.SelectedValue);//为单选题对象各属性赋值
            //singleproblem.Title = txtTitle.Text;
            //singleproblem.AnswerA = txtAnswerA.Text;
            //singleproblem.AnswerB = txtAnswerB.Text;
            //singleproblem.AnswerC = txtAnswerC.Text;
            //singleproblem.AnswerD = txtAnswerD.Text;
            //singleproblem.Answer = ddlAnswer.SelectedItem.Text;
            //if (Request["ID"] != null)                                  //如果是修改题目信息
            //{
            //    singleproblem.ID = int.Parse(Request["ID"].ToString()); //取出试题主键
            //    //if (singleproblem.UpdateByProc(int.Parse(Request["ID"].ToString())))//调用修改试题方法修改试题
            //    //{
            //    //    Response.Write("<script>alert('修改成功！');</script>");
            //    //}
            //    //else
            //    //{
            //    //    Response.Write("<script>alert('修改失败！');</script>");
            //    //}
            //}


            if (Request["ID"] != null)                                  //如果是修改题目信息
            {
                using (DataClassesDataContext ds = new DataClassesDataContext())
                {
                    int SingleProblemID = int.Parse(Request["ID"].ToString());  //取出传递过来的试题编号
                    var r = (from q in ds.SingleProblem
                             where q.ID == SingleProblemID
                             select q).FirstOrDefault();
                    r.c_id = int.Parse(ddlCourse.SelectedValue);//为单选题对象各属性赋值
                    r.Title = txtTitle.Text;
                    r.AnswerA = txtAnswerA.Text;
                    r.AnswerB = txtAnswerB.Text;
                    r.AnswerC = txtAnswerC.Text;
                    r.AnswerD = txtAnswerD.Text;
                    r.Answer = ddlAnswer.SelectedItem.Text;
                    ds.SubmitChanges();

                    Response.Write("<script>alert('修改成功！');</script>");
                }
            }
            else
            {
                Response.Write("<script>alert('修改失败！');</script>");
            }

        }
    }
}